package GameRoom;

import blackjack.Card;
import blackjack.Deck;
import people.Dealer;
import people.Player;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class GameRoom {
    Dealer curDealer;
    Player curPlayer;
    Deck curDeck;

    enum operate {
        Double, Draw, Stop, Error
    }

    GameRoom(){
        curDealer = new Dealer();
        curPlayer = new Player(Constant.ID, Constant.STARTMONEY);
        curDeck = new Deck();
        // Initialize the Internal Variable

        curDeck.shuffle();
    }
    GameRoom(String id, int startMoney){
        curDealer = new Dealer();
        curPlayer = new Player(id, startMoney);
        curDeck = new Deck();
        // Initialize the Internal Variable

        curDeck.shuffle();
    }

    private operate getOperation() throws IOException {
        System.out.println("请输入数字以确认操作：\n0：加倍当前下注并抽卡\n1：抽一张卡\n2：开牌 ");
        Scanner scanner = new Scanner(System.in);
        operate nextOperation;
        switch (scanner.nextInt()){
            case 0:
                nextOperation = operate.Double;
                break;
            case 1:
                nextOperation = operate.Draw;
                break;
            case 2:
                nextOperation = operate.Stop;
                break;
            default:
                nextOperation = operate.Error;
                break;
        }
//        BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
//        bfr.skip(Constant.INF);
        return nextOperation;
    }

//    private void printCard(Card card){
//        System.out.println("抽到的卡为：" + card.getSuit() + ' ' + card.getRank());
//    }
    public int run() throws IOException {

        System.out.println(curPlayer.getID()+"当前剩余金钱：" + curPlayer.getMoney());
        //1 玩家输入下注金额
        System.out.println("请输入下注金额：");
        Scanner scanner = new Scanner(System.in);
        if(curPlayer.addBet(scanner.nextInt()) == 1){
            do{
                System.out.println("余额不足或数额小于等于0，请重新设置下注金额：");
            }while (curPlayer.addBet(scanner.nextInt()) == 1);
        }

        // 1.5 庄家进行第一次抽卡
        curDealer.getTwoCards(curDeck);
        System.out.println("庄家抽2张卡。第一张卡是：");
        //printCard(curDealer.getHand().displayOne());
        curDealer.getHand().displayOne().displayCard();
        // 2 玩家抽卡并进行操作
        System.out.println(curPlayer.getID() + " 抽2张卡：");
        curPlayer.getTwoCards(curDeck);
        System.out.println(curPlayer.getID()+"你的当前手牌为：");
        curPlayer.getHand().displayHand();

        while(true){
            boolean leave = false;
            switch(getOperation()){
                case Double:
                    System.out.println(curPlayer.getID() + " 选择操作：加倍并抽卡");
                    if(curPlayer.addBet(curPlayer.getBet()) == 1){
                        System.out.println(curPlayer.getID()+"剩余金额不足~");
                    }
                    else{
                        System.out.println(curPlayer.getID()+"当前下注金额：" + curPlayer.getBet());
                        Card nextCard = curDeck.drawCard();
                        curPlayer.addCard(nextCard);
                        System.out.println(curPlayer.getID()+"你的当前手牌为：");
                        curPlayer.getHand().displayHand();

                        if(curPlayer.getBusted()) {
                            System.out.println(curPlayer.getID()+"Boom！");
                            leave = true;
                        }
                    }
                    break;
                case Draw:
                    System.out.println(curPlayer.getID() + " 选择操作：抽卡");
                    Card nextCard = curDeck.drawCard();
                    curPlayer.addCard(nextCard);
                    System.out.println(curPlayer.getID()+"你的当前手牌为：");
                    curPlayer.getHand().displayHand();

                    if(curPlayer.getBusted()) {
                        System.out.println("Boom！");
                        leave = true;
                    }
                    break;
                case Stop:
                    System.out.println(curPlayer.getID() + " 选择操作：停止");
                    leave = true;
                    break;
                default:
                    System.out.println(curPlayer.getID()+"输入操作非法，请重新输入~");
                    break;
            }
            if(leave){
                System.out.println(curPlayer.getID()+"你的点数为：" + curPlayer.getBestPoint());
                break;
            }
        }


        // 3 庄家进行抽卡环节
        System.out.println("\n庄家开始抽卡");
        curDealer.supplementCards(curDeck);
        System.out.println("庄家抽卡结果：");
        curDealer.getHand().displayHand();
        System.out.println("庄家点数为：" + curDealer.getBestPoint());

        // 4 计算胜负
        int playerScore = 0, dealerScore = 0, res = 0;  // res 用于指示游戏结果，1代表玩家胜利，-1代表庄家胜利，0代表平局。
        if(curPlayer.getHand().isBlackJack()) playerScore += Constant.INF;
        if(curDealer.getHand().isBlackJack()) dealerScore += Constant.INF;
        if(curPlayer.getHand().isBusted()) playerScore -= Constant.INF;
        if(curDealer.getHand().isBusted()) dealerScore -= Constant.INF;
        playerScore += curPlayer.getBestPoint();
        dealerScore += curDealer.getBestPoint();


        if(curPlayer.getBusted() && curDealer.getBusted()){
            res = 0;
        } else if (playerScore != dealerScore) {
            res = (playerScore > dealerScore) ? 1 : -1;
        }

        if(res == 1){
            System.out.println("玩家 " + curPlayer.getID() + " 胜利！");
            if(curPlayer.getHand().isBlackJack()) {
                System.out.println("由于BlackJack，赢得的金额变为1.5倍");
                curPlayer.addMoney((int)(curPlayer.getBet() * Constant.RATE) + curPlayer.getBet());
                System.out.println("你赢得了" + (int)(curPlayer.getBet() * Constant.RATE) + "元");
            }
            else {
                curPlayer.addMoney(curPlayer.getBet() + curPlayer.getBet());
                System.out.println("你赢得了" + curPlayer.getBet() + "元");
            }
            curPlayer.emptyBet();
        }
        else if(res == -1){
            System.out.println("庄家胜利！");
            System.out.println("你失去了" + curPlayer.getBet() + "元");
            //curPlayer.loseMoney(curPlayer.getBet());
            curPlayer.emptyBet();
        }
        else{
            System.out.println("平局！");
            curPlayer.addMoney(curPlayer.getBet());
            curPlayer.emptyBet();
        }
        System.out.println(curPlayer.getID() + " 当前剩余金钱: " + curPlayer.getMoney());
        return curPlayer.getMoney();
    }
    public void resetGame(){
        Player newPlayer = new Player(curPlayer.getID(), curPlayer.getMoney());
        Dealer newDealer = new Dealer();
        Deck newDeck = new Deck();
        newDeck.shuffle();

        curPlayer = newPlayer;
        curDealer = newDealer;
        curDeck = newDeck;
    }

    public static void main(String[] args) throws IOException {
        int moneyLeft = 0;
        System.out.println("欢迎来到21点游戏！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请为自己取一个响亮的名字：");
        String playerName = scanner.nextLine();
        System.out.println("请先充值金额：");
        int initialMoney = scanner.nextInt();
        while(initialMoney<=0){
            System.out.println("金额应为正数，请重新充值！");
            initialMoney = scanner.nextInt();
        }

        GameRoom gr = new GameRoom(playerName, initialMoney);
        moneyLeft = gr.run();
        while(moneyLeft > 0){
            System.out.println("再来一轮？ 0：同意 1：结束");
            int res = scanner.nextInt();
            if (res == 1) break;
            else if (res == 0) {
                gr.resetGame();
                moneyLeft = gr.run();
                if (moneyLeft <= 0) break;
            }
            else {
                System.out.println("非法输入，请输入数字1或0！");
            }
        }
    }
}
